{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "## 4.2 通用函数：快速逐元素级别的数组计算函数 Universal Functions: Fast Element-wise Array Functions\r\n",
    "\r\n",
    "通用函数（Universal Function, ufunc）：对ndarray中的数据执行元素级别运算的函数\r\n",
    "\r\n",
    "+ 一元通用函数（Unary ufunc）\r\n",
    "+ 二元通用函数（Binary ufunc）"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# 模块导入\r\n",
    "import os, sys\r\n",
    "sys.path.append(os.path.dirname(os.getcwd()))\r\n",
    "import numpy\r\n",
    "from dependency import arr_info"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 4.2.1 一元通用函数\r\n",
    "\r\n",
    "+ `numpy.abs()`，`numpy.fabs()`：计算整数的绝对值，计算浮点数的绝对值；\r\n",
    "+ `numpy.sqrt()`：计算各元素的平方根。相当于 `arr**0.5`；\r\n",
    "+ `numpy.square()`：计算个元素的平方。相当于 `arr**2`；\r\n",
    "+ `numpy.exp()`：计算各元素的e指数；\r\n",
    "+ `numpy.log()`，`numpy.log10()`，`numpy.log2()`，`numpy.log1p()`：计算各元素的对数。注意：`numpy.log()` 计算以自然对数为底的对数；`numpy.log1p()`计算 log(1+x) 的对数；\r\n",
    "+ `numpy.sign()`：符号函数\r\n",
    "+ `numpy.ceil()`：计算各元素的ceiling值，即大于等于该值的最小整数\r\n",
    "+ `numpy.floor()`：计算各元素的floor值，即小于等于该值的最小整数\r\n",
    "+ `numpy.rint()`：将各元素“五舍六入”至整数，保留dtype\r\n",
    "+ `numpy.modf()`：将数组的小数和整数部分以两个独立数组的形式返回\r\n",
    "+ `numpy.isnan()`：返回一个布尔数组，标识元素是否为 `NaN` 类型（不是一个数）。是 `NaN` 类型则为 `True`，否则为 `False`\r\n",
    "+ `numpy.isfinite()`，`numpy.isinf()`：返回一个布尔数组，标识元素是否为有穷尽的（finite），或无穷尽的（infinite）\r\n",
    "+ `numpy.sin()`，`numpy.cos()`，`numpy.tan()`：计算各元素的三角函数\r\n",
    "+ `numpy.sinh()`，`numpy.cosh()`，`numpy.tanh()`：计算各元素的双曲三角函数\r\n",
    "+ `numpy.arcsin()`，`numpy.arccos()`，`numpy.arctan()`：计算各元素的反三角函数\r\n",
    "+ `numpy.arcsinh()`，`numpy.arccosh()`，`numpy.arctanh()`：计算各元素的反双曲三角函数\r\n",
    "+ `numpy.logical_not()`：计算各元素 `not x` 的真值，相当于 `-arr`\r\n"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# 一元通用函数\r\n",
    "\r\n",
    "array1_1 = numpy.arange(12).reshape((3,4))\r\n",
    "array1_2 = numpy.array([ [1.0, 1.2, 1.4, 1.6, 1.8], [2.1, 2.3, 2.5, 2.7, 2.9] ])\r\n",
    "\r\n",
    "arr_sqrt = numpy.sqrt(array1_1)\r\n",
    "arr_rint = numpy.rint(array1_2)\r\n",
    "\r\n",
    "arr_info([ array1_1, arr_sqrt ])\r\n",
    "arr_info([ array1_2, arr_rint ])"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 4.2.2 二元通用函数\r\n",
    "\r\n",
    "+ `numpy.add()`：将两数组对应元素相加（数组shape需相同）；\r\n",
    "+ `numpy.subtract()`：从第一个数组中减去第二个数组对应的元素；\r\n",
    "+ `numpy.multiply()`：两数组对应元素相乘；\r\n",
    "+ `numpy.divide()`，`numpy.floor_divide()`：两数组对应元素相除，向下圆整相除（丢弃余数）；\r\n",
    "+ `numpy.power()`：以第一个数组中的元素为底数，第二个数组中对应的元素为指数，计算乘方；\r\n",
    "+ `numpy.maximum()`，`numpy.fmax()`：对应元素最大值，`fmax()`将忽略`NaN`；\r\n",
    "+ `numpy.minimum()`，`numpy.fmin()`：对应元素最小值，`fmin()`将忽略`NaN`；\r\n",
    "+ `numpy.mod()`：对应元素求模运算（除法的余数）；\r\n",
    "+ `numpy.copysign()`：将第二个数组中元素的符号，复制给第一个数组对应元素；\r\n",
    "+ `numpy.greater()`，`numpy.greater_equal()`，`numpy.less()`，`numpy.less_equal()`，`numpy.equal()`，`numpy.not_equal()`：返回布尔数组，对应元素作比较运算。即：`>`、`>=`、`<`、`<=`、`==`、`=!`；\r\n",
    "+ `numpy.logical_and()`，`numpy.logical_or()`，`numpy.logical_xor()`：对应元素执行真值逻辑运算。即：`&`、`|`、`^`；\r\n"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# 二元通用函数\r\n",
    "\r\n",
    "array1_3 = numpy.random.randn(12).reshape((3,4))\r\n",
    "array1_4 = numpy.random.randn(12).reshape((3,4))\r\n",
    "\r\n",
    "arr_add = numpy.add(array1_3, array1_4)\r\n",
    "arr_power = numpy.power(array1_3, 2)        # power() 函数的参数也可以为标量\r\n",
    "\r\n",
    "arr_info([ array1_3, array1_4 ])\r\n",
    "arr_info([ arr_add, arr_power ])"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# 特殊函数：modf() 返回多个数组\r\n",
    "\r\n",
    "arr_modf = numpy.modf(array1_2)\r\n",
    "\r\n",
    "# arr_info([arr_modf])  # 由于返回值为元组，调用此函数报错：\"Tuple\" object has no attribute \"ndim\"\r\n",
    "print(arr_modf)"
   ],
   "outputs": [],
   "metadata": {}
  }
 ],
 "metadata": {
  "orig_nbformat": 4,
  "language_info": {
   "name": "python",
   "version": "3.9.6",
   "mimetype": "text/x-python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "pygments_lexer": "ipython3",
   "nbconvert_exporter": "python",
   "file_extension": ".py"
  },
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.9.6 64-bit ('DataAnalysis': venv)"
  },
  "interpreter": {
   "hash": "0fe90ac710750590bd916650f441b3b2233faa2658175d6302b22e90f225e38d"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}